package com.sx.micowell.module.member

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.bar.TitleBar
import com.example.mvvm.base.ContainerActivity
import com.example.mvvm.base.action.TitleBarAction
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.LogUtils
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar
import com.sx.micowell.R
import com.sx.micowell.databinding.FragPaymentQrCodeBinding
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RoutePath
import com.uuzuche.lib_zxing.activity.CodeUtils
import org.json.JSONObject

/**
 * @author: YY
 * @time: 2021/8/9
 * @describe 二维码支付
 */
@Route(path = RoutePath.PAY_QR_CODE)
class PayQrCodeFragment : MyFragment<FragPaymentQrCodeBinding, ContainerActivity>(), TitleBarAction {

    @Autowired(name = IntentKey.DATA)
    @JvmField
    var qrData: String? = null

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.frag_payment_qr_code)
    }

    override fun initView() {
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()
        binding.title.setOnTitleBarListener(this)
    }

    override fun initData() {
        if (qrData != null) {
            LogUtils.d("initData: $qrData")
            val json = JSONObject(qrData!!)
            convertBitmap(json.getString("code_url"))
        }
    }

    /**
     * 转换二维码
     */
    private fun convertBitmap(bitmapStr: String) {
        val width = resources.getDimension(R.dimen.dp_186).toInt()
        val height = resources.getDimension(R.dimen.dp_186).toInt()
        val qrBitmap = CodeUtils.createImage(
            bitmapStr, width, height, null)
        if (qrBitmap != null) {
            binding.imagePayQrCode.setImageBitmap(qrBitmap)
        }
    }

    override fun getTitleBar(): TitleBar? {
        return binding.title
    }

    override fun onLeftClick(v: View?) {
        super.onLeftClick(v)
        myActivity.finish()
    }
}